package com.eryos.android.cigarettecounter.charts;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.net.UnknownHostException;
import java.text.DateFormat;
import java.util.Locale;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.widget.Toast;

import com.eryos.android.cigarettecounter.R;

/**
 * Project :  CigaretteCounter
 * Author :   Olivier Combe 
 * URL :      http://eryos.fr/CigaretteCounter/
 *
 * Copyright (C) 2011 - Olivier Combe 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 *
 */

public abstract class Chart {
	
	private Context context;

	protected final static String ROOT_URL 		= "http://chart.apis.google.com/chart?";

	public static final int DEFAULT_CHART_WIDTH = 320;
	public static final int DEFAULT_CHART_HEIGHT = 200;

	
	public static final String DATA_SEPARATOR 	= ",";
	public static final String LABEL_SEPARATOR 	= "|";

	protected String type 			= "lc";
	
	protected int width 			= 320;
	protected int height 			= 200;
	protected String bgColor 		= "000000";
	protected String title 			= null;
	protected String titleColor 	= "FFFFFF";
	protected double titleSize 		= 12.5;
	protected double textSize		= 10.5;
	protected String axisColor      = "FFFFFF";
	
	protected String data 			= "";

	protected int maxYValue 		= 22;
	protected int minYValue 		= 0;
	protected int maxXValue 		= 31;
	protected int minXValue 		= 0;

	protected String URL;
	
	public Chart(){
		
	}
	
	public String getURL(){
		generateURL();
		
		return URL;
	}
	
	protected abstract void generateURL();


	public int getWidth() {
		return width;
	}


	public void setWidth(int _width) {		
		this.width = _width;
	}


	public int getHeight() {
		return height;
	}


	public void setHeight(int _height) {
		this.height = _height;
	}


	public String getBgColor() {
		return bgColor;
	}


	public void setBgColor(String bgColor) {
		this.bgColor = bgColor;
	}


	public String getTitle() {
		return title;
	}


	public void setTitle(String title) {
		this.title = title;
	}


	public String getTitleColor() {
		return titleColor;
	}


	public void setTitleColor(String titleColor) {
		this.titleColor = titleColor;
	}


	public String getData() {
		return data;
	}


	public void setData(String _data) {
		
    	if ( _data.length() > DATA_SEPARATOR.length() && _data.endsWith(DATA_SEPARATOR) )
    		_data = _data.substring(0, _data.length()- Chart.DATA_SEPARATOR.length());

		this.data = _data;
	}


	public int getMaxYValue() {
		return maxYValue;
	}


	public void setMaxYValue(int maxYValue) {
		this.maxYValue = maxYValue;
	}


	public int getMinYValue() {
		return minYValue;
	}


	public void setMinYValue(int minYValue) {
		this.minYValue = minYValue;
	}


	public int getMaxXValue() {
		return maxXValue;
	}


	public void setMaxXValue(int maxXValue) {
		this.maxXValue = maxXValue;
	}


	public int getMinXValue() {
		return minXValue;
	}


	public void setMinXValue(int minXValue) {
		this.minXValue = minXValue;
	}

	public double getTitleSize() {
		return titleSize;
	}

	public void setTitleSize(double titleSize) {
		textSize = (titleSize*0.8);
		this.titleSize = titleSize;
	}
	
    
    
	/* ********************************************************************** */
	/*                  STATIC   METHODS                                      */
	/* ********************************************************************** */

    
    private static boolean msgShown = false;

    public static Bitmap downloadChartAsBitmap(Context ctx, String url){
    	Log.i("downloadBitmap", url);
        Bitmap bm = null;
        try{
            URL aURL = new URL(url);
            URLConnection conn = aURL.openConnection();
            conn.connect();
            InputStream is = conn.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(is);
            bm = BitmapFactory.decodeStream(bis);
            bis.close();
            is.close();
            Log.d("downloadBitmap","OK");
            return bm;
        } catch(UnknownHostException e){
        	if ( ! msgShown ){ 
        		Toast.makeText(ctx, "No available connection, please retry later ...", Toast.LENGTH_SHORT).show();
        		msgShown = true;
        	}
        	Log.w("downloadBitmap", "No available connection");
            return getDefaultChartPicture(ctx);
        } catch(IOException e){
            Log.e("downloadBitmap", "Erreur IO", e);
            return getDefaultChartPicture(ctx);
        }
    }
    

    public static Bitmap downloadChartAsBitmap(String url) throws UnknownHostException, IOException {
    	Log.i("downloadBitmap", url);
        Bitmap bm = null;
        try{
            URL aURL = new URL(url);
            URLConnection conn = aURL.openConnection();
            conn.connect();
            InputStream is = conn.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(is);
            
            bm = BitmapFactory.decodeStream(bis);
            
            bis.close();
            is.close();
            
            Log.d("downloadBitmap","OK");
            return bm;
        } catch(UnknownHostException e){
        	Log.w("downloadBitmap", "No available connection");
            throw e;
        } catch(IOException e){
            Log.e("downloadBitmap", "Erreur IO", e);
            throw e;
        }
    }
    
    public static Bitmap getDefaultChartPicture(Context ctx){
    	Drawable errorDrawable = ctx.getResources().getDrawable(R.drawable.ic_menu_discard);
        return ((BitmapDrawable)errorDrawable).getBitmap();
    }
}
